
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.HashMap;
import java.util.Scanner;


public class Test {

    public static void main(String[] args) throws Exception {
        //HashMap<String, V> vs = new HashMap<>();
        HashMap<String, ConnectList> connectListHashMap = new HashMap<>();
        File FileDir = new File(args[0]);
        Scanner scan = new Scanner(FileDir);
        String current = scan.nextLine();
        while (current != null && (!"".equals(current))) {
            if(current.startsWith("i")){
                String[] data = current.split("\t");
                String intersectionId = data[1];
                double latitude = Double.parseDouble(data[2]);
                double longitude = Double.parseDouble(data[3]);

                if (latitude < Graph.minLatitude) {
                    Graph.minLatitude = latitude;
                }
                if (latitude > Graph.maxLatitude) {
                    Graph.maxLatitude = latitude;
                }
                if (longitude < Graph.minLongitude) {
                    Graph.minLongitude = longitude;
                }
                if (longitude > Graph.maxLongitude) {
                    Graph.maxLongitude = longitude;
                }
                Intersection item = new Intersection(intersectionId, longitude, latitude, Double.MAX_VALUE, false, null);
                Graph.allIntersectionMap.put(item.IntersectionId, item);
                //vs.put(item.IntersectionId,new V(item.IntersectionId));
                ConnectList temp = new ConnectList();
                temp.init(item);
                //insert the intersectionID and the linked list into the main connectListHashMap
                connectListHashMap.put(item.IntersectionId, temp);
                current = scan.nextLine();
            }else if(current.startsWith("r")){
                String data[] = current.split("\t");
                String roadID = data[1];
                String int1 = data[2];
                String int2 = data[3];
                Intersection w = Graph.allIntersectionMap.get(int1);
                Intersection x = Graph.allIntersectionMap.get(int2);
                double distance = Util.calculateDistance(w, x);
                Road road = new Road(roadID, int1, int2, distance);
                Graph.allRoads.add(road);
                if (scan.hasNextLine() == false) {
                    break;
                }
                ConnectList connectList1 = connectListHashMap.get(road.intersect1);
                ConnectList connectList2 = connectListHashMap.get(road.intersect2);
                connectList1.connectRoad(road);
                connectList2.connectRoad(road);
                current = scan.nextLine();
            }

        }

        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-show")) {
                Graph.isShow = true;
            }
            if (args[i].equals("-directions")) {
                Graph.isDijkstras = true;
                Graph.begin = args[i + 1];
                Graph.end = args[i + 2];
            }
            if (args[i].equals("-meridianmap")) {
                Graph.isMinTree = true;
            }
        }


        //求最小树
        if (Graph.isMinTree) {
//            HashMap<Integer, Intersection> int2Index = new HashMap<>();
//            HashMap<String, Road> roadHashMap = new HashMap<>();
//            HashMap<String, Integer> index2Int = new HashMap<>();
//            int i = 1;
//            for (Map.Entry<String, Intersection> entry : Graph.allIntersectionMap.entrySet()) {
//                int2Index.put(i, entry.getValue());
//                index2Int.put(entry.getKey(), i);
//                i++;
//            }
//            HashMap<String, Double> prim = new HashMap();
//            for (Road road : Graph.allRoads) {
//                roadHashMap.put(road.intersect1 + road.intersect2, road);
//                roadHashMap.put(road.intersect2 + road.intersect1, road);
//                prim.put(index2Int.get(road.intersect1) + "," + index2Int.get(road.intersect2), road.distance);
//                prim.put(index2Int.get(road.intersect2) + "," + index2Int.get(road.intersect1), road.distance);
//            }
//            index2Int.put("0", 0);
//            for (Map.Entry<String, Integer> entry : index2Int.entrySet()) {
//                for (Map.Entry<String, Integer> entry2 : index2Int.entrySet()) {
//
//                    if (!prim.containsKey(entry.getValue() + "," + entry2.getValue())) {
//                        prim.put(entry.getValue() + "," + entry2.getValue(), Double.MAX_VALUE);
//                    }
//                }
//                System.out.println(prim.size());
//            }
            //Graph.minTress = Prim.prim(i - 1, int2Index, roadHashMap);
            //G g = new G();
//            for (Map.Entry<String, V> entry : vs.entrySet()) {
//                g.addV(entry.getValue());
//            }
//            for (Road road: Graph.allRoads){
//                g.add(new E(vs.get(road.intersect1),vs.get(road.intersect2),road.distance));
//            }
            //Graph.minTress = MinTree.kruskal(g, roadHashMap);
            Graph.minTress = MinTree.kruskals();

        }
        //求最短路径
        if (Graph.isDijkstras) {
//            HashMap<Integer, Intersection> int2Index = new HashMap<>();
//            Intersection[] vertexes = new Intersection[Graph.allIntersectionMap.size()];
            HashMap<String, Road> roadHashMap = new HashMap<>();
//            HashMap<String, Integer> index2Int = new HashMap<>();
//            int i = 0;
//            for (Map.Entry<String, Intersection> entry : Graph.allIntersectionMap.entrySet()) {
//                int2Index.put(i, entry.getValue());
//                vertexes[i] = entry.getValue();
//                index2Int.put(entry.getKey(), i);
//                i++;
//            }
            //double[][] prim = new double[i][i];
            for (Road road : Graph.allRoads) {
                roadHashMap.put(road.intersect1 + road.intersect2, road);
                roadHashMap.put(road.intersect2 + road.intersect1, road);
                //prim[index2Int.get(road.intersect1)][index2Int.get(road.intersect2)] = road.distance;
                //prim[index2Int.get(road.intersect2)][index2Int.get(road.intersect1)] = road.distance;
            }
//            for (int i1 = 0; i1 < prim.length; i1++) {
//                for (int j = 0; j < prim[i1].length; j++) {
//                    if (prim[i1][j] == 0 && i1 != j) {
//                        prim[i1][j] = Double.MAX_VALUE;
//                    }
//                }
//            }
            Graph.dijkstras = Dijkstra.dijkstra(connectListHashMap, roadHashMap);

        }

        if (Graph.isShow) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Graph graph = new Graph();
            graph.setPreferredSize(new Dimension(1500, 1200));
            frame.getContentPane().add(graph);
            frame.pack();
            frame.setVisible(true);
        }
        scan.close();
    }

}
